#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void chu_shi1(int x, int y, int(*arr)[50])
{
    int a = 0;
    int b = 0;
    for (a = 0; a <= x + 1; a++)
    {
        for (b = 0; b <= y + 1; b++)
        {
            arr[a][b] = 1;
        }
    }
}
void chu_shi2(int x, int y, int(*arr)[50])
{
    int a = 0;
    int b = 0;
    for (a = 1; a <= x; a++)
    {
        for (b = 1; b <= y; b++)
        {
            arr[a][b] = 0;
        }
    }
}
    
    int shu_ru(int x, int y, int(*arr)[50])
    {
        int a = 0;
        int b = 0;
        for (a = 1; a <= x; a++)
        {
            for (b = 1; b <= y; b++)
            {
                scanf_s("%d", &arr[a][b]);
            }
        }
        return 0;
    }
    int count(int x, int y, int(*arr)[50])
    {
        int a = 0;
        int b = 0;
        int c = 0;
        for (a = 1; a <= x; a++)
        {
            for (b = 1; b <= y; b++)
            {
                if (2 == arr[a][b])
                {
                    if (0 == arr[a - 1][b])
                    {
                        c++;
                        arr[a - 1][b] = 1;
                    }
                    if (0 == arr[a + 1][b])
                    {
                        c++;
                        arr[a + 1][b] = 1;
                    }
                    if (0 == arr[a][b - 1])
                    {
                        c++;
                        arr[a][b - 1] = 1;
                    }
                    if (0 == arr[a][b + 1])
                    {
                        c++;
                        arr[a][b + 1] = 1;
                    }
                }
            }
        }
        return c;
    }
    int main()
    {
        int d = 0;
        int m = 0;
        int n = 0;
        int qi_pan[50][50] = { 0 };
        int(* pqi_pan)[50] = qi_pan;
        scanf_s("%d %d", &n, &m);
        chu_shi1(n, m, pqi_pan);
        chu_shi2(n, m, pqi_pan);
        shu_ru(n, m, pqi_pan);
        d = count(n, m, pqi_pan);
        printf("%d", d);
        return 0;
    }